import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, ViewController } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { DomSanitizer } from '@angular/platform-browser';
import { HelpDataService } from '../../providers/service/help-data.service';
import { Network } from '@ionic-native/network';

/**
 * Generated class for the HelpPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-help',
  templateUrl: 'help.html',
})
export class HelpPage {

	// 页数
	page =1;
	//有网状态=1 断网=0
	Internet = 1;
  constructor(
		public navCtrl: NavController,
		public viewCtrl: ViewController,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public helpdata: HelpDataService,
		private network: Network,
		private sanitizer: DomSanitizer) {}

   help : Help[] = new Array<Help>();

  async ionViewDidLoad() {
		console.log('ionViewDidLoad HelpPage');
		if(this.network.type!=='none' && this.network.type !== '2g'){
			this.Internet=1;
			this.getHelp();
		}
		if(this.network.type ==='none' || this.network.type === '2g'){
			this.Internet=0;
			let a = await this.helpdata.find((help) => help.key !=='');
			this.help=a;
		}
  }


  assembleHTML(strHTML:any) {
	return this.sanitizer.bypassSecurityTrustHtml(strHTML);
	}

  getHelp() {
		this.http.postJson('', 'system/softwen/getsoftwen', 
		     {
          "tables":"5",                 
          "file_type_id":"8",
          "soft_wen_type":"3",
          "limit":"2",
          "page":"1"
		     }, '').then(res => {
		     console.log(res)
			if(res['status'] == 200) {
			this.getMessage(res['data']);
			}
		});
		
	}
	getMessage(help) {
		this.help.splice(0);
		console.log(help)
		for (let message of help) {
			if (message) {
				var a = {
					/*根据提供的接口地址传参数,配置文件在http.ts里查看,里面写的是链接服务器的配置*/
					  img:  this.http.APP_IMG+message.url,
				    titles : message.titles,
            description: message.description,
				};
				this.help.push(a);
			}
		}
		this.helpdata.deleteAll();
		for(var i=0; i<this.help.length; i++){
			this.helpdata.save(this.help[i]);
		}

		console.log(this.help);
	 }

	 //无限滚动(下滑加载)
	 async  lodeMessage(infiniteScroll) {
    this.page++;
    const res = await this.http.postJson('', 'system/softwen/getsoftwen',
      {
				"tables":"5",                 
				"file_type_id":"8",
				"soft_wen_type":"3",
				"limit":"2",
				"page" :this.page,
      }, '').then(res => {
        console.log(res)
        if (res['status'] == 200) {
					for (let message of res['data']) {
						if (message) {
							var a = {
								/*根据提供的接口地址传参数,配置文件在http.ts里查看,里面写的是链接服务器的配置*/
									img:  this.http.APP_IMG+message.url,
									titles : message.titles,
									description: message.description,
							};
							this.help.push(a);
						}
					}
          setInterval(infiniteScroll.complete(), 500);
        }
      });
  }
}

export class Help{
	img:any;
	titles:any;
	description:any;
}
